package com.example.mall.user.svc.interfaces;

import com.example.mall.common.model.Result;
import com.example.mall.user.svc.app.cmd.BindCmd;
import com.example.mall.user.svc.app.cmd.CreateUserCmd;
import com.example.mall.user.svc.app.service.UserAppService;
import com.example.mall.user.svc.app.service.UserQryService;
import com.example.mall.user.svc.domain.model.entity.User;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Mickey
 * @since 2022/4/15
 **/
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final UserAppService userAppService;
    private final UserQryService userQryService;

    @PostMapping("/create")
    public Result<Long> create(@RequestBody CreateUserCmd createUser) {
        return Result.OK(userAppService.create(createUser));
    }

    @PutMapping("/bind")
    public Result<?> bind(@RequestBody BindCmd<?> bindMobile) {
        userAppService.bind(bindMobile);
        return Result.OK();
    }

    @GetMapping("/list")
    public Result<List<User>> list() {
        return Result.OK(userQryService.list());
    }

    @GetMapping("/byUniqueId")
    public Result<User> byUniqueId(String uniqueId) {
        return Result.OK(userQryService.loadOrCreate(uniqueId, null).orElse(null));
    }

}
